﻿/*
 * Class  :  LibGlobal
 * Author : Chung Thành Phước 
 * Create Date: 18/12/2014
 * Update date: 18-12-2014 by Chung Thành Phước 
 * Reference: System.Configuration,System.Data,System.Data.SqlClient,System.IO,System.Web
 */


using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using System.Web;

namespace LibGlobal
{
    public class ImportExcel
    {
        #region[Define]
        private static string connectionString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
        private static readonly string pathFolderRoot = ConfigurationManager.AppSettings["FolderRoot"];
        public static OleDbConnection excel_con;
        public static SqlBulkCopy sqlBulkCopy;
        public static DataTable dtTable;
        #endregion

        #region[Get-Connect]
        private static void GetConnectionExcel(string folder, string myFile)
        {
            var excelPath = HttpContext.Current.Server.MapPath(pathFolderRoot + folder + "/") + Path.GetFileName(myFile);
            connectionString = string.Format(connectionString, excelPath);
            excel_con = new OleDbConnection(connectionString);
            excel_con.Open();
        }
        #endregion

        #region[Get-Data]
        public static DataTable GetData(string folder, string myFile)
        {
            GetConnectionExcel(folder, myFile);
            var oleDbSchemaTable = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            DataTable result;
            result = oleDbSchemaTable;
            excel_con.Close();
            return result;
        }
        #endregion
    }
}
